package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.PageModel;
import cn.com.cfae.iras.doc.analyze.parse.model.SectionModel;
import cn.com.cfae.iras.doc.analyze.parse.model.WordItem;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 房地产
 */
public class FDCTokenizer extends BaseTokenizer {

    private static FDCTokenizer instance = new FDCTokenizer();


    public static FDCTokenizer getInstance() {
        return instance;
    }

    private FDCTokenizer() {
    }

    private static Logger logger = LoggerFactory.getLogger(FDCTokenizer.class);

    public boolean splitWords(ExtractItem extractItem, String text) {
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(text);
        List<IWord> wordList = sentence.wordList;
        logger.info("开始分析抽取房地产行业特色业务指标，指标名称：{}，文本：{}，分词列表：{}。", extractItem.getItemName(), text, wordList);
        IWord iWord = null;
        int offset = -1;
        boolean isCandicate = false;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();

            if (ArrayUtils.contains(extractItem.getSynonyms(), word.trim())) {
                KVText kText = new KVText();
                kText.setText(iWord.getValue());
                extractItem.setKey(kText);
                isCandicate = true;
                logger.info("分析到房地产行业特色业务指标，指标名称：{}，文本内容：{}，ItemName：{}，索引位置：{}。", extractItem.getItemName(), word, extractItem.getItemName(), offset);
                break;
            }
        }
        return isCandicate;
    }


    public void extractFDC_XSJE(ExtractItem extractItem, SectionModel childSectionModel, List<PageModel> fragmentPageModelList) {
        logger.info("开始分析抽取房地产行业业务模型数据，指标名称：{}......", extractItem.getItemName());
        long t1 = System.currentTimeMillis();
        String extractText = childSectionModel.getContent();
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(extractText);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        Map<String, ExtractItem> extractItemMap = new HashMap<>();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，分析文本：{}，列表：{}。", extractItem.getItemName(), extractText, wordList);
        int offset = -1;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();
            if (ArrayUtils.contains(extractItem.getSynonyms(), word)) {
                offset = i;
                if (!extractItemMap.containsKey(extractItem.getItemName())) {
                    extractItemMap.put(extractItem.getItemName(), extractItem);
                    extractItem.setItemValue(iWord.getValue());
                    logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据，业务指标名称：{}，分析文本：{}。", extractItem.getItemName(), word);
                    continue;
                }
                break;
            }
        }
        if (offset == -1) {
            return;
        }
        WordItem targetWordItem = null;
        int thresholdValue = 20;
        List<WordItem> wordItemList = null;
        for (Iterator<String> it = extractItemMap.keySet().iterator(); it.hasNext(); ) {
            wordItemList = new ArrayList<>();
            String key = it.next();
            ExtractItem var1 = extractItemMap.get(key);
            String prefixRegex = "((" + joinSynonyms(extractItem.getSynonyms()) + ")|((([0-9]{0,3}\\,{1,}){0,}([0-9]{1,3}){1,}\\.{0,}[0-9]{0,2})\\s{0,}(万元|亿元){1}))";
            Pattern pattern = Pattern.compile(prefixRegex);
            Matcher matcher = pattern.matcher(extractText);
            WordItem wordItem;
            while (matcher.find()) {
                wordItem = new WordItem();
                wordItem.setValue(matcher.group());
                wordItem.setOffset(matcher.start());
                wordItemList.add(wordItem);
            }
            Collections.sort(wordItemList);
            logger.info("分析抽取房地产行业业务模型数据，输出提取的业务指标内容，指标名称：{}。", var1.getItemName());
            wordItemList.forEach(e -> {
                logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据文本信息，分析文本：{}，偏移位置：{}。", e.getValue(), e.getOffset());
            });
            for (int i = 0; i < wordItemList.size(); i++) {
                WordItem wordItem1 = wordItemList.get(i);
                if (ArrayUtils.contains(var1.getSynonyms(), wordItem1.getValue())) {
                    KVText kText = extractItem.getKey();
                    if (extractItem.getKey() == null) {
                        kText = new KVText();
                        TextLayout kTextLayout = new TextLayout();
                        kTextLayout.setText(kText.getText());
                        var1.setKey(kText);
                        kText.add(kTextLayout);
                    }
                    targetWordItem = wordItem1;
                    kText.setText(wordItem1.getValue());
                    var1.getKey().getTextLayout().setText(var1.getKey().getText());
                }
                if (isFDC_XSJE_UNIT(wordItem1.getValue())) {
                    if (targetWordItem == null) {
                        continue;
                    }
                    if (targetWordItem.getOffset() > wordItem1.getOffset()) {
                        continue;
                    }
                    if (wordItem1.getOffset() - targetWordItem.getOffset() > thresholdValue) {
                        continue;
                    }
                    KVText vText = new KVText();
                    vText.setText(wordItem1.getValue());
                    TextLayout vTextLayout = null;
                    try {
                        vTextLayout = findTextLayout(wordItem1.getValue(), fragmentPageModelList);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                    if (vTextLayout != null) {
                        vTextLayout.setText(wordItem1.getValue());
                    } else {
                        vTextLayout = new TextLayout();
                        vTextLayout.setPageNumber(childSectionModel.getTextLayout().getPageNumber());
                        vTextLayout.setTop(childSectionModel.getTextLayout().getTop());
                        vTextLayout.setLeft(childSectionModel.getTextLayout().getLeft());
                    }
                    vTextLayout.setText(vText.getText());
                    vText.add(vTextLayout);
                    var1.addValue(vText);
                }
            }
        }
        long t2 = System.currentTimeMillis();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，耗时：{}毫秒。", extractItem.getItemName(), (t2 - t1));
    }

    public void extractFDC_QYXSMJ(ExtractItem extractItem, SectionModel childSectionModel, List<PageModel> fragmentPageModelList) {
        logger.info("开始分析抽取房地产行业业务模型数据，指标名称：{}......", extractItem.getItemName());
        long t1 = System.currentTimeMillis();
        String extractText = childSectionModel.getContent();
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(extractText);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        Map<String, ExtractItem> extractItemMap = new HashMap<>();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，分析文本：{}，分词列表：{}。", extractItem.getItemName(), extractText, wordList);
        int offset = -1;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();
            if (ArrayUtils.contains(extractItem.getSynonyms(), word)) {
                offset = i;
                if (!extractItemMap.containsKey(extractItem.getItemName())) {
                    extractItemMap.put(extractItem.getItemName(), extractItem);
                    extractItem.setItemValue(iWord.getValue());
                    logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据，业务指标名称：{}，分析文本：{}。", extractItem.getItemName(), word);
                    continue;
                }
                break;
            }
        }
        if (offset == -1) {
            return;
        }
        WordItem targetWordItem = null;
        int thresholdValue = 20;
        List<WordItem> wordItemList = null;
        for (Iterator<String> it = extractItemMap.keySet().iterator(); it.hasNext(); ) {
            wordItemList = new ArrayList<>();
            String key = it.next();
            ExtractItem var1 = extractItemMap.get(key);
            String prefixRegex = "((" + joinSynonyms(extractItem.getSynonyms()) + ")|((([0-9]{0,3}\\,{1,}){0,}([0-9]{1,3}){1,}\\.{0,}[0-9]{0,2})\\s{0,}(万平方米){1}))";
            Pattern pattern = Pattern.compile(prefixRegex);
            Matcher matcher = pattern.matcher(extractText);
            WordItem wordItem;
            while (matcher.find()) {
                wordItem = new WordItem();
                wordItem.setValue(matcher.group());
                wordItem.setOffset(matcher.start());
                wordItemList.add(wordItem);
            }
            Collections.sort(wordItemList);
            logger.info("分析抽取房地产行业业务模型数据，输出提取的业务指标内容，指标名称：{}。", var1.getItemName());
            wordItemList.forEach(e -> {
                logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据文本信息，分析文本：{}，偏移位置：{}。", e.getValue(), e.getOffset());
            });

            for (int i = 0; i < wordItemList.size(); i++) {
                WordItem wordItem1 = wordItemList.get(i);
                if (ArrayUtils.contains(var1.getSynonyms(), wordItem1.getValue())) {
                    KVText kText = extractItem.getKey();
                    if (extractItem.getKey() == null) {
                        kText = new KVText();
                        TextLayout kTextLayout = new TextLayout();
                        kTextLayout.setText(kText.getText());
                        var1.setKey(kText);
                        kText.add(kTextLayout);
                    }
                    targetWordItem = wordItem1;
                    kText.setText(wordItem1.getValue());
                    var1.getKey().getTextLayout().setText(var1.getKey().getText());
                }
                if (isFDC_QYXSMJ_UNIT(wordItem1.getValue())) {
                    if (targetWordItem == null) {
                        continue;
                    }
                    if (targetWordItem.getOffset() > wordItem1.getOffset()) {
                        continue;
                    }
                    if (wordItem1.getOffset() - targetWordItem.getOffset() > thresholdValue) {
                        continue;
                    }
                    KVText vText = new KVText();
                    vText.setText(wordItem1.getValue());
                    TextLayout vTextLayout = null;
                    try {
                        vTextLayout = findTextLayout(wordItem1.getValue(), fragmentPageModelList);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                    if (vTextLayout != null) {
                        vTextLayout.setText(wordItem1.getValue());
                    } else {
                        vTextLayout = new TextLayout();
                        vTextLayout.setPageNumber(childSectionModel.getTextLayout().getPageNumber());
                        vTextLayout.setTop(childSectionModel.getTextLayout().getTop());
                        vTextLayout.setLeft(childSectionModel.getTextLayout().getLeft());
                    }
                    vTextLayout.setText(vText.getText());
                    vText.add(vTextLayout);
                    var1.addValue(vText);
                }
            }
        }
        long t2 = System.currentTimeMillis();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，耗时：{}毫秒。", extractItem.getItemName(), (t2 - t1));
    }

    public void extractFDC_TDCBGHJZMJ(ExtractItem extractItem, SectionModel childSectionModel, List<PageModel> fragmentPageModelList) {
        logger.info("开始分析抽取房地产行业业务模型数据，指标名称：{}......", extractItem.getItemName());
        long t1 = System.currentTimeMillis();
        String extractText = childSectionModel.getContent();
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(extractText);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        Map<String, ExtractItem> extractItemMap = new HashMap<>();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，分析文本：{}，列表：{}。", extractItem.getItemName(), extractText, wordList);
        int offset = -1;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();
            if (ArrayUtils.contains(extractItem.getSynonyms(), word)) {
                offset = i;
                if (!extractItemMap.containsKey(extractItem.getItemName())) {
                    extractItemMap.put(extractItem.getItemName(), extractItem);
                    extractItem.setItemValue(iWord.getValue());
                    logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据，业务指标名称：{}，分析文本：{}。", extractItem.getItemName(), word);
                    continue;
                }
                break;
            }
        }
        if (offset == -1) {
            return;
        }
        WordItem targetWordItem = null;
        int thresholdValue = 20;
        List<WordItem> wordItemList = null;
        for (Iterator<String> it = extractItemMap.keySet().iterator(); it.hasNext(); ) {
            wordItemList = new ArrayList<>();
            String key = it.next();
            ExtractItem var1 = extractItemMap.get(key);
            String prefixRegex = "((" + joinSynonyms(extractItem.getSynonyms()) + ")|((([0-9]{0,3}\\,{1,}){0,}([0-9]{1,3}){1,}\\.{0,}[0-9]{0,2})\\s{0,}(平方米|万平方米){1}))";
            Pattern pattern = Pattern.compile(prefixRegex);
            Matcher matcher = pattern.matcher(extractText);
            WordItem wordItem;
            while (matcher.find()) {
                wordItem = new WordItem();
                wordItem.setValue(matcher.group());
                wordItem.setOffset(matcher.start());
                wordItemList.add(wordItem);
            }
            Collections.sort(wordItemList);
            logger.info("分析抽取房地产行业业务模型数据，输出提取的业务指标内容，指标名称：{}。", var1.getItemName());
            wordItemList.forEach(e -> {
                logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据文本信息，分析文本：{}，偏移位置：{}。", e.getValue(), e.getOffset());
            });

            for (int i = 0; i < wordItemList.size(); i++) {
                WordItem wordItem1 = wordItemList.get(i);
                if (ArrayUtils.contains(var1.getSynonyms(), wordItem1.getValue())) {
                    KVText kText = extractItem.getKey();
                    if (extractItem.getKey() == null) {
                        kText = new KVText();
                        TextLayout kTextLayout = new TextLayout();
                        kTextLayout.setText(kText.getText());
                        var1.setKey(kText);
                        kText.add(kTextLayout);
                    }
                    targetWordItem = wordItem1;
                    kText.setText(wordItem1.getValue());
                    var1.getKey().getTextLayout().setText(var1.getKey().getText());
                }

                if (isFDC_TDCBGHZBMJ_UNIT(wordItem1.getValue())) {
                    if (targetWordItem == null) {
                        continue;
                    }
                    if (targetWordItem.getOffset() > wordItem1.getOffset()) {
                        continue;
                    }
                    if (wordItem1.getOffset() - targetWordItem.getOffset() > thresholdValue) {
                        continue;
                    }
                    KVText vText = new KVText();
                    vText.setText(wordItem1.getValue());
                    TextLayout vTextLayout = null;
                    try {
                        vTextLayout = findTextLayout(wordItem1.getValue(), fragmentPageModelList);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                    if (vTextLayout != null) {
                        vTextLayout.setText(wordItem1.getValue());
                    } else {
                        vTextLayout = new TextLayout();
                        vTextLayout.setPageNumber(childSectionModel.getTextLayout().getPageNumber());
                        vTextLayout.setTop(childSectionModel.getTextLayout().getTop());
                        vTextLayout.setLeft(childSectionModel.getTextLayout().getLeft());
                    }
                    vTextLayout.setText(vText.getText());
                    vText.add(vTextLayout);
                    var1.addValue(vText);
                }
            }
        }
        long t2 = System.currentTimeMillis();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，耗时：{}毫秒。", extractItem.getItemName(), (t2 - t1));
    }

    public void extractFDC_DSMJZB(ExtractItem extractItem, SectionModel childSectionModel, List<PageModel> fragmentPageModelList) {
        logger.info("开始分析抽取房地产行业业务模型数据，指标名称：{}......", extractItem.getItemName());
        long t1 = System.currentTimeMillis();
        String extractText = childSectionModel.getContent();
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(extractText);
        List<IWord> wordList = sentence.wordList;
        IWord iWord = null;
        Map<String, ExtractItem> extractItemMap = new HashMap<>();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，分析文本：{}，列表：{}。", extractItem.getItemName(), extractText, wordList);
        int offset = -1;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();
            if (ArrayUtils.contains(extractItem.getSynonyms(), word)) {
                offset = i;
                if (!extractItemMap.containsKey(extractItem.getItemName())) {
                    extractItemMap.put(extractItem.getItemName(), extractItem);
                    extractItem.setItemValue(iWord.getValue());
                    logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据，业务指标名称：{}，分析文本：{}。", extractItem.getItemName(), word);
                    continue;
                }
                break;
            }
        }
        if (offset == -1) {
            return;
        }
        List<WordItem> wordItemList = null;
        WordItem targetWordItem = null;
        int thresholdValue = 20;
        for (Iterator<String> it = extractItemMap.keySet().iterator(); it.hasNext(); ) {
            wordItemList = new ArrayList<>();
            String key = it.next();
            ExtractItem var1 = extractItemMap.get(key);
            String prefixRegex = "((" + joinSynonyms(extractItem.getSynonyms()) + ")|(([0-9]{0,2})\\s{0,}(%){1}))";
            Pattern pattern = Pattern.compile(prefixRegex);
            Matcher matcher = pattern.matcher(extractText);
            WordItem wordItem;
            while (matcher.find()) {
                wordItem = new WordItem();
                wordItem.setValue(matcher.group());
                wordItem.setOffset(matcher.start());
                wordItemList.add(wordItem);
            }
            Collections.sort(wordItemList);
            logger.info("分析抽取房地产行业业务模型数据，输出提取的业务指标内容，指标名称：{}。", var1.getItemName());
            wordItemList.forEach(e -> {
                logger.info("分析抽取房地产行业业务模型数据，分析定位到业务模型数据文本信息，分析文本：{}，偏移位置：{}。", e.getValue(), e.getOffset());
            });
            for (int i = 0; i < wordItemList.size(); i++) {
                WordItem wordItem1 = wordItemList.get(i);
                if (ArrayUtils.contains(var1.getSynonyms(), wordItem1.getValue())) {
                    KVText kText = extractItem.getKey();
                    if (extractItem.getKey() == null) {
                        kText = new KVText();
                        TextLayout kTextLayout = new TextLayout();
                        kTextLayout.setText(kText.getText());
                        var1.setKey(kText);
                        kText.add(kTextLayout);
                    }
                    targetWordItem = wordItem1;
                    kText.setText(wordItem1.getValue());
                    var1.getKey().getTextLayout().setText(var1.getKey().getText());
                }
                if (isFDC_DSZBMJ_UNIT(wordItem1.getValue())) {
                    if (targetWordItem == null) {
                        continue;
                    }
                    if (targetWordItem.getOffset() > wordItem1.getOffset()) {
                        continue;
                    }
                    if (wordItem1.getOffset() - targetWordItem.getOffset() > thresholdValue) {
                        continue;
                    }
                    KVText vText = new KVText();
                    vText.setText(wordItem1.getValue());
                    TextLayout vTextLayout = null;
                    try {
                        vTextLayout = findTextLayout(wordItem1.getValue(), fragmentPageModelList);
                    } catch (Exception e) {
                        logger.error(e.getMessage(), e);
                    }
                    if (vTextLayout != null) {
                        vTextLayout.setText(wordItem1.getValue());
                    } else {
                        vTextLayout = new TextLayout();
                        vTextLayout.setPageNumber(childSectionModel.getTextLayout().getPageNumber());
                        vTextLayout.setTop(childSectionModel.getTextLayout().getTop());
                        vTextLayout.setLeft(childSectionModel.getTextLayout().getLeft());
                    }
                    vTextLayout.setText(vText.getText());
                    vText.add(vTextLayout);
                    var1.addValue(vText);
                }
            }
        }
        long t2 = System.currentTimeMillis();
        logger.info("分析抽取房地产行业业务模型数据，指标名称：{}，耗时：{}毫秒。", extractItem.getItemName(), (t2 - t1));
    }
}